<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.contenttypes.models &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django" href="../../../django.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-contenttypes-models">
            
  <h1>Source code for django.contrib.contenttypes.models</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="kn">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.db.utils</span> <span class="kn">import</span> <span class="n">OperationalError</span><span class="p">,</span> <span class="n">ProgrammingError</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">smart_text</span><span class="p">,</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">python_2_unicode_compatible</span>


<div class="viewcode-block" id="ContentTypeManager"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager">[docs]</a><span class="k">class</span> <span class="nc">ContentTypeManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>

    <span class="c"># Cache to avoid re-looking up ContentType objects all over the place.</span>
    <span class="c"># This cache is shared by all the get_for_* methods.</span>
    <span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>

<div class="viewcode-block" id="ContentTypeManager.get_by_natural_key"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key">[docs]</a>    <span class="k">def</span> <span class="nf">get_by_natural_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">][(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="p">)]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ct</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ct</span>
</div>
    <span class="k">def</span> <span class="nf">_get_opts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">for_concrete_model</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">for_concrete_model</span><span class="p">:</span>
            <span class="n">model</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span>
        <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">_deferred</span><span class="p">:</span>
            <span class="n">model</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">proxy_for_model</span>
        <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

    <span class="k">def</span> <span class="nf">_get_from_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">opts</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">][</span><span class="n">key</span><span class="p">]</span>

<div class="viewcode-block" id="ContentTypeManager.get_for_model"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager.get_for_model">[docs]</a>    <span class="k">def</span> <span class="nf">get_for_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">for_concrete_model</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the ContentType object for a given model, creating the</span>
<span class="sd">        ContentType if necessary. Lookups are cached so that subsequent lookups</span>
<span class="sd">        for the same model don&#39;t hit the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_opts</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">for_concrete_model</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_from_cache</span><span class="p">(</span><span class="n">opts</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">ct</span><span class="p">,</span> <span class="n">created</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span>
                    <span class="n">app_label</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                    <span class="n">model</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
                    <span class="n">defaults</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="n">smart_text</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name_raw</span><span class="p">)},</span>
                <span class="p">)</span>
            <span class="k">except</span> <span class="p">(</span><span class="n">OperationalError</span><span class="p">,</span> <span class="n">ProgrammingError</span><span class="p">):</span>
                <span class="c"># It&#39;s possible to migrate a single app before contenttypes,</span>
                <span class="c"># as it&#39;s not a required initial dependency (it&#39;s contrib!)</span>
                <span class="c"># Have a nice error for this.</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                    <span class="s">&quot;Error creating new content types. Please make sure contenttypes&quot;</span> <span class="o">+</span>
                    <span class="s">&quot; is migrated before trying to migrate apps individually.&quot;</span>
                <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ct</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">ct</span>
</div>
<div class="viewcode-block" id="ContentTypeManager.get_for_models"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager.get_for_models">[docs]</a>    <span class="k">def</span> <span class="nf">get_for_models</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">models</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given *models, returns a dictionary mapping {model: content_type}.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">for_concrete_models</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;for_concrete_models&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
        <span class="c"># Final results</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c"># models that aren&#39;t already in the cache</span>
        <span class="n">needed_app_labels</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">needed_models</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="n">needed_opts</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_opts</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">for_concrete_models</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_from_cache</span><span class="p">(</span><span class="n">opts</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">needed_app_labels</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">)</span>
                <span class="n">needed_models</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
                <span class="n">needed_opts</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">opts</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">results</span><span class="p">[</span><span class="n">model</span><span class="p">]</span> <span class="o">=</span> <span class="n">ct</span>
        <span class="k">if</span> <span class="n">needed_opts</span><span class="p">:</span>
            <span class="n">cts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                <span class="n">app_label__in</span><span class="o">=</span><span class="n">needed_app_labels</span><span class="p">,</span>
                <span class="n">model__in</span><span class="o">=</span><span class="n">needed_models</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">ct</span> <span class="ow">in</span> <span class="n">cts</span><span class="p">:</span>
                <span class="n">model</span> <span class="o">=</span> <span class="n">ct</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span> <span class="ow">in</span> <span class="n">needed_opts</span><span class="p">:</span>
                    <span class="n">results</span><span class="p">[</span><span class="n">model</span><span class="p">]</span> <span class="o">=</span> <span class="n">ct</span>
                    <span class="n">needed_opts</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ct</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">opts</span> <span class="ow">in</span> <span class="n">needed_opts</span><span class="p">:</span>
            <span class="c"># These weren&#39;t in the cache, or the DB, create them.</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
                <span class="n">app_label</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="n">model</span><span class="o">=</span><span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="n">smart_text</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name_raw</span><span class="p">),</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ct</span><span class="p">)</span>
            <span class="n">results</span><span class="p">[</span><span class="n">ct</span><span class="o">.</span><span class="n">model_class</span><span class="p">()]</span> <span class="o">=</span> <span class="n">ct</span>
        <span class="k">return</span> <span class="n">results</span>
</div>
<div class="viewcode-block" id="ContentTypeManager.get_for_id"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager.get_for_id">[docs]</a>    <span class="k">def</span> <span class="nf">get_for_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Lookup a ContentType by ID. Uses the same shared cache as get_for_model</span>
<span class="sd">        (though ContentTypes are obviously not created on-the-fly by get_by_id).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">][</span><span class="nb">id</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c"># This could raise a DoesNotExist; that&#39;s correct behavior and will</span>
            <span class="c"># make sure that only correct ctypes get stored in the cache dict.</span>
            <span class="n">ct</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="nb">id</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ct</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ct</span>
</div>
<div class="viewcode-block" id="ContentTypeManager.clear_cache"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentTypeManager.clear_cache">[docs]</a>    <span class="k">def</span> <span class="nf">clear_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Clear out the content-type cache. This needs to happen during database</span>
<span class="sd">        flushes to prevent caching of &quot;stale&quot; content type IDs (see</span>
<span class="sd">        django.contrib.contenttypes.management.update_contenttypes for where</span>
<span class="sd">        this gets called).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
</div>
    <span class="k">def</span> <span class="nf">_add_to_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">ct</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Insert a ContentType into the cache.&quot;&quot;&quot;</span>
        <span class="c"># Note it&#39;s possible for ContentType objects to be stale; model_class() will return None.</span>
        <span class="c"># Hence, there is no reliance on model._meta.app_label here, just using the model fields instead.</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">ct</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">ct</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">using</span><span class="p">,</span> <span class="p">{})[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">ct</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">using</span><span class="p">,</span> <span class="p">{})[</span><span class="n">ct</span><span class="o">.</span><span class="n">id</span><span class="p">]</span> <span class="o">=</span> <span class="n">ct</span>

</div>
<span class="nd">@python_2_unicode_compatible</span>
<div class="viewcode-block" id="ContentType"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentType">[docs]</a><span class="k">class</span> <span class="nc">ContentType</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">app_label</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;python model class name&#39;</span><span class="p">),</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">ContentTypeManager</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">verbose_name</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;content type&#39;</span><span class="p">)</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;content types&#39;</span><span class="p">)</span>
        <span class="n">db_table</span> <span class="o">=</span> <span class="s">&#39;django_content_type&#39;</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,)</span>
        <span class="n">unique_together</span> <span class="o">=</span> <span class="p">((</span><span class="s">&#39;app_label&#39;</span><span class="p">,</span> <span class="s">&#39;model&#39;</span><span class="p">),)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># self.name is deprecated in favor of using model&#39;s verbose_name, which</span>
        <span class="c"># can be translated. Formal deprecation is delayed until we have DB</span>
        <span class="c"># migration to be able to remove the field from the database along with</span>
        <span class="c"># the attribute.</span>
        <span class="c">#</span>
        <span class="c"># We return self.name only when users have changed its value from the</span>
        <span class="c"># initial verbose_name_raw and might rely on it.</span>
        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">model</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_raw</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>

<div class="viewcode-block" id="ContentType.model_class"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentType.model_class">[docs]</a>    <span class="k">def</span> <span class="nf">model_class</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the Python model class for this type of content.&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">LookupError</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
</div>
<div class="viewcode-block" id="ContentType.get_object_for_this_type"><a class="viewcode-back" href="../../../../ref/contrib/contenttypes.html#django.contrib.contenttypes.models.ContentType.get_object_for_this_type">[docs]</a>    <span class="k">def</span> <span class="nf">get_object_for_this_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns an object of this type for the keyword arguments given.</span>
<span class="sd">        Basically, this is a proxy around this object_type&#39;s get_object() model</span>
<span class="sd">        method. The ObjectNotExist exception, if thrown, will not be caught,</span>
<span class="sd">        so code that calls this method should catch it.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">get_all_objects_for_this_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns all objects of this type for the keyword arguments given.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">natural_key</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
        <ul><li>django.contrib.contenttypes.models</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>